package it.isa.demo.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Corgile
 * @date 2022-02-26
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
	/*
	@Bean
	public MappingJackson2HttpMessageConverter mapping() {
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
		ObjectMapper om = new ObjectMapper();
		om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		converter.setObjectMapper(om);
		return converter;
	}
	*/

	/*@Bean
	public ObjectMapper two() {
		ObjectMapper om = new ObjectMapper();
		om.setDateFormat(new SimpleDateFormat("MM-dd-yyyy"));
		return om;
	}*/

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/user/**")
				.allowedOrigins("http://localhost:8081")
				.allowedHeaders("*")
				.allowedMethods("*")
				.maxAge(30 * 1000);
	}

	/**
	 * 配置fastjson的解析器
	 * */
	@Bean
	FastJsonHttpMessageConverter fastJsonConverter() {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setDateFormat("yyyy-MM-dd");
		converter.setFastJsonConfig(config);
		return converter;
	}
}
